package com.taxi.remote;

import com.taxi.constant.AmapConfigConstants;
import com.taxi.response.PointDTO;
import com.taxi.response.PointResponse;
import com.taxi.result.Result;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

import java.net.URI;

/**
 * @Author: aniu
 * @Date: 2023/10/1
 * @ClassName: PointClient
 * @Description: Write class functionality and purpose here.
 * @Version: 1.0
 */
@Service
public class PointClient {
    @Value("${amap.key}")
    private String amapKey;

    @Value("${amap.sid}")
    private String amapSid;

    @Autowired
    private RestTemplate restTemplate;

    public Result upload(PointResponse pointResponse) {
        try {
            //拼装请求的url
            StringBuilder url = new StringBuilder();
            url.append(AmapConfigConstants.POINT_UPLOAD);
            url.append("?");
            url.append("key="+amapKey);
            url.append("&");
            url.append("sid="+amapSid);
            url.append("&");
            url.append("tid="+pointResponse.getTid());  // 终端id
            url.append("&");
            url.append("trid="+pointResponse.getTrid()); // 轨迹id
            url.append("&");
            url.append("points=");

            PointDTO[] points = pointResponse.getPoints();
            url.append("%5B");
            for (PointDTO point : points) {
                url.append("%7B");
                url.append("%22location%22");
                url.append("%3A");
                url.append("%22"+point.getLocation()+"%22");
                url.append("%2C");
                url.append("%22locatetime%22");
                url.append("%3A");
                url.append(point.getLocatetime());
                url.append("%7D");
            }
            url.append("%5D");

            ResponseEntity<String> stringResponseEntity = restTemplate.postForEntity(URI.create(url.toString()), null, String.class);

            JSONObject result = JSONObject.fromObject(stringResponseEntity.getBody());
            System.out.println("轨迹点上传---");
            return Result.ok(result);
        } catch (RestClientException e) {
            return Result.fail(e.getMessage());
        }
    }
}
